<?php
/**
 * @version $Id: view.html.php 48 2011-09-28 06:47:49Z lnmtrung $
*/
 
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.view' );

class WrdViewAtransaction extends WrdView{
	
	function display($tpl = null) {
		
		$this->setModel($this->getModel('atransaction'), true);
		$fnc = 'display'.ucfirst($this->getLayout());
		if (is_callable(array(&$this, $fnc)))
			$this->$fnc($tpl);
		else
			parent::display($tpl);
	}

	function displayDefault($tpl = null){
		
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		
		$document = & JFactory::getDocument();
		$document->setTitle( JText::_('Transactions') );

		JToolBarHelper::title(JText::_('Transactions'),'transaction.png');
 		JToolBarHelper::addNew();
		JToolBarHelper::deleteList();
		
		WrdHelper::addSubMenu('config',$view);
		$this->sidebar = JHtmlSidebar::render();

		$filter_order     		= $mainframe->getUserStateFromRequest( $context.$view.'filter_order_item','filter_order','id' );
		$filter_order_Dir 		= $mainframe->getUserStateFromRequest( $context.$view.'filter_order_Dir','filter_order_Dir', '' );

		$lists['order'] 		= $filter_order;
		$lists['order_Dir'] 	= $filter_order_Dir;
		
		$items					= & $this->get( 'Data');
		$user_profile = array() ;
		$user_name = array() ;
		$outlet_name = array() ;
		foreach($items as $tran){
			//$user_name[$tran->vip_number] = WrdHelper::getUsernameByMemberId($tran->vip_number) ;
            $user_name[$tran->vip_number] = WrdHelper::getUserProfilesByMemberId($tran->vip_number) ;
            $user_name[$tran->vip_number] = $user_name[$tran->vip_number]->l_name .' '.$user_name[$tran->vip_number]->f_name;
			$outlet_name[$tran->outlet] = WrdHelper::getUsernameByOutletCode($tran->outlet) ;            
			$user_profile[$tran->vip_number] = WrdHelper::getUserProfile($tran->vip_number, 'member_id') ;
		}	
		$total					= & $this->get( 'Total');
		$pagination 			= & $this->get( 'Pagination' );
		 
		$search 				= $mainframe->getUserStateFromRequest( $context.$view.'search','search','','string' );
		$filter_state 			= $mainframe->getUserStateFromRequest( $context.$view.'published','published',-1,'int' );
		$filter_user			= $mainframe->getUserStateFromRequest( $context.$view.'created_id','created_id',-1,'int' );
		
		$search 				= JString::strtolower( $search );
		
		$lists['search'] 		= $search;
		$lists['user'] 			= JHTML::_('wrd.listUser','created_id',$filter_user,'onchange="this.form.submit()"','atransaction');

		$this->assignRef('search',		$search);
	    $this->assignRef('lists',		$lists);
	  	$this->assignRef('items',		$items);
	  	$this->assignRef('user_name',	$user_name);
	  	$this->assignRef('outlet_name',	$outlet_name);
	  	$this->assignRef('user_profile',	$user_profile);
	  	
	    $this->assignRef('pagination',	$pagination);
	    
	    parent::display($tpl);
  	}
  
  	function displayForm($tpl = null){
		
		$mainframe = &JFactory::getApplication();
		$context = &JRequest::getCmd('option');
		$view = &JRequest::getCmd('view');
		$user = &JFactory::getUser();
		
		$cid = &JRequest::getInt('id');
		$tid = &JRequest::getInt('tid');
		$vip_num  = &JRequest::getInt('vip_num');
		
		$model	= & $this->getModel();
		$model_outlet = &JModelLegacy::getInstance('outlets','WrdModel');                   
		$detail = array();
		
		if($cid != 0)
		{
			$detail	= &$model->getItem($cid);
			$point_detail = $this->getPointItem($detail->transaction_id);
		}
		else{
			$detail	= &$model->getItem($tid,'transaction');
			$point_detail = $this->getPointItem($tid);
		}
		//echo "<pre>";print_r($detail);
  		$mid = JRequest::getInt('vip_num');  		
		if(empty($mid)){
			 $mid = $detail->vip_number;
			 $forward = 0;
		}else{
			$forward = 1;
		}
		
		$user_info = null ;
		$user_info = WrdHelper::getUserProfile($mid, 'memberid') ; 
		
		//Get point latest of user from updatesummaryPoint
		$q = 'SELECT user_id,year,month,point FROM #__wrd_points_summary WHERE user_id='.$user_info->user_id .' ORDER BY year DESC,month DESC';
		$db = JFactory::getDbo();
		$db->setQuery($q);
		$infotest = $db->loadObjectList();
		$this->assignRef('infotest',$infotest);
		//end get point
		
		if(!empty($user_info->user_id)){
			$outlet = $model_outlet->getItem($user_info->pick_up_at);
		}
		
		$lists = array();
		$lists['published'] 		= JHTML::_('wrd.published',$detail->published,'',0);
		$lists['outlets'] 		= JHTML::_('wrd.listOutletInAddTransaction','outlet',$detail->outlet,'','outlet',''," and pid != '00' AND `code` != '' ");

		$text = ($detail->id < 1) ? JText::_( 'NEW' ) : JText::_( 'EDIT' );
		JToolBarHelper::title(   JText::_( 'Transactions' ).': <small><small>['.$text.']</small></small>','transaction.png' );
		JToolBarHelper::save();
		JToolBarHelper::cancel( 'cancel', 'Close' );

		$this->assignRef('data',		$detail);
		$this->assignRef('lists',		$lists);
		$this->assignRef('user',		$user);
		$this->assignRef('vip_num',		$vip_num);
		$this->assignRef('user_info',	$user_info);
		$this->assignRef('point_detail',	$point_detail);
		
		
		$this->assignRef('outlet',	$outlet);
		$this->assignRef('forward',	$forward);

		$this->assignRef('job',$user_info->o_job);
		parent::display($tpl);
	}
	
	function getPointItem($transaction_id){
		$db = &JFactory::getDbo();
		$query = 'SELECT * FROM #__wrd_point where transaction = '.$transaction_id.' AND type = 0';
		
		return $db->setQuery($query)->loadObject();
	}
}
?>